package com.nf.health.app.http;

import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue.RequestFilter;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.nf.health.app.MyApplication;
import com.nf.health.app.core.HttpActionHandle;
import com.nf.health.app.utils.JsonUtils;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;

import java.util.Map;

public class HttpRequestProvider {
	private static final String TAG = HttpRequestProvider.class.getSimpleName();
	private HttpActionHandle actionHandle;
	private String actionName;
	public HttpRequestProvider(HttpActionHandle actionHandle, String actionName) {
		this.actionHandle = actionHandle;
		this.actionName = actionName;
	}

	/**
	 * 取消请求
	 * 
	 * @param tag
	 */
	public void cancelRequest(String tag) {
		LogUtil.i(TAG, "----cancelRequest----" + tag + actionHandle.hashCode());
		MyApplication.getRequestQueue().cancelAll(actionHandle.hashCode() + tag);
	}

	/**
	 * 取消所有请求
	 */
	public void cancelAllRequest() {
		LogUtil.i(TAG, "----cancleAllRequest----");
		MyApplication.getRequestQueue().cancelAll(new RequestFilter() {
			@Override
			public boolean apply(Request<?> request) {
				return (int) request.getTag() == actionHandle.hashCode();
			}
		});
	}

	/**
	 * 请求数据
	 * 
	 * @param method
	 * @param map
	 * @param url
	 * @param dataParse
	 */
	public void request(int method, Map<String, String> map, String url, final DataParse dataParse) {
		actionHandle.handleActionStart(actionName, null);
		map.put("version", Apis.VERSION);
		map.put("access_token", PreferenceHelper.getString(PreferenceHelper.ACCESS_TOKEN, ""));
		map.put("platform", "android");
		HttpStringRequest.getInstance().request(actionHandle, method, url, map, null, new Listener<String>() {

			@Override
			public void onResponse(String response) {
				if (response != null) {
					if (JsonUtils.isSuccess(response)) {
						try {
							dataParse.onParse(response);
						} catch (Exception e) {
							actionHandle.handleActionError(actionName, "小康出问题啦~555", null);
							LogUtil.exception(e);
						}
					} else {
						actionHandle.handleActionError(actionName, response, null);
					}
				}
				actionHandle.handleActionFinish(actionName, null);
			}
		}, new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				actionHandle.handleActionError(actionName, null, error);
				actionHandle.handleActionFinish(actionName, null);
			}
		});

	}


	/**
	 * 请求数据
	 * @param method
	 * @param map
	 * @param mapHeader
	 * @param url
	 * @param dataParse
	 */
	public void request(int method, Map<String, String> map, Map<String, String> mapHeader,String url, final DataParse dataParse) {
		actionHandle.handleActionStart(actionName, null);
		map.put("version", Apis.VERSION);
		map.put("access_token", PreferenceHelper.getString(PreferenceHelper.ACCESS_TOKEN, ""));
		map.put("platform","android");

		HttpStringRequest.getInstance().request(actionHandle, method, url, map, mapHeader, new Listener<String>() {
			@Override
			public void onResponse(String response) {
				if (response != null) {
					if (JsonUtils.isSuccess(response)) {
						try {
							dataParse.onParse(response);
						} catch (Exception e) {
							actionHandle.handleActionError(actionName, "小康出问题啦~555", null);
							e.printStackTrace();
							LogUtil.exception(e);
						}
					} else {
						actionHandle.handleActionError(actionName, response, null);
					}
				}
				actionHandle.handleActionFinish(actionName, null);
			}
		}, new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				actionHandle.handleActionError(actionName, null, error);
				actionHandle.handleActionFinish(actionName, null);
			}
		});
		LogUtil.i(TAG, url + "");
	}
	/**
	 * 支付 请求方式不同 另复制一行
	 */
	public void request2(int method, Map<String, String> map, Map<String, String> mapHeader,String url, final DataParse dataParse) {
		actionHandle.handleActionStart(actionName, null);

		HttpStringRequest.getInstance().request(actionHandle, method, url+"&version="+Apis.VERSION+"&access_token="+PreferenceHelper.getString(PreferenceHelper.ACCESS_TOKEN, "")+"&platform=android", null, mapHeader, new Listener<String>() {
			@Override
			public void onResponse(String response) {
				if (response != null) {
					if (JsonUtils.isSuccess(response)) {
						try {
							dataParse.onParse(response);
						} catch (Exception e) {
							actionHandle.handleActionError(actionName, "小康出问题啦~555", null);
							e.printStackTrace();
							LogUtil.exception(e);
						}
					} else {
						actionHandle.handleActionError(actionName, response, null);
					}
				}
				actionHandle.handleActionFinish(actionName, null);
			}
		}, new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				actionHandle.handleActionError(actionName, null, error);
				actionHandle.handleActionFinish(actionName, null);
			}
		});
		LogUtil.i(TAG,url+"");
	}

	// 解析数据
	public interface DataParse {
		void onParse(String result) throws com.google.gson.JsonParseException;
	}

}
